#!/usr/bin/ruby

require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'classes'

class GraphTest < Test::Unit::TestCase

	# Pattern: test_<class name>_<method name>

	def test_array_pp
		a = []
		3.times { a << Vertex.new('A') }
		assert_equal ['A','A','A'].inspect, a.pp 
	end

	def test_array_copy
		a = [1,2,3,4]
		assert_equal a, a.copy
	end
	
	def test_graph_initialize
		g = Graph.new
		assert_kind_of Graph, g
		assert_equal [], g.get_vertices
	end

	def test_graph_add_vertex
		g = Graph.new
		v = Vertex.new('A')
		g.add_vertex(v)
		assert g.get_vertices.include?(v)
	end

	def test_graph_get_vertices
		v = []
		5.times { v << Vertex.new }
		g = Graph.new(v)
		assert_equal v, g.get_vertices
	end

	def test_graph_n
		v = []
		5.times { v << Vertex.new }
		g = Graph.new(v)
		assert_equal 5, g.n
	end

	def test_graph_get_vertex
		v = []
		5.times { v << Vertex.new }
		g = Graph.new(v)
		assert_equal v[3], g.get_vertex(3)
	end

	def test_vertex_initialize
		v = Vertex.new
		assert_kind_of Vertex, v
	end

	def test_vertex_connect_to
		v = Vertex.new
		u = Vertex.new
		assert !v.get_neighbors.include?(u)
		v.connect_to(u)
		assert v.get_neighbors.include?(u)
	end
	
	def test_vertex_is_connected_to
		v = Vertex.new
		u = Vertex.new
		assert !v.is_connected_to?(u)
		v.connect_to(u)
		assert v.is_connected_to?(u)
	end

	def test_vertex_get_neighbors
		v = Vertex.new
		u = Vertex.new
		w = Vertex.new
		v.connect_to(u)
		v.connect_to(w)
		assert_equal [u,w], v.get_neighbors
	end

	def test_vertex_get_label
		v = Vertex.new('A')
		assert_equal 'A', v.get_label
	end

	def test_vertex_closest_prior_neighbors
		v = Vertex.new
		u = Vertex.new
		w = Vertex.new
		x = Vertex.new
		set = [v,u,w,x]
		v.connect_to(w)
		v.connect_to(x)
		assert_equal w, v.closest_prior_neighbor(set)
	end

	def test_graph_print_matrix
		v = Vertex.new('v')
		u = Vertex.new('u')
		w = Vertex.new('w')
		x = Vertex.new('x')
		g = Graph.new([u,v,w,x])
		[[u,w],[u,x],[v,w],[w,u],[w,v],[x,u]].each do |pair|
			pair[0].connect_to(pair[1])
		end
		assert_equal "  u v w x\nu 0 0 1 1 \nv 0 0 1 0 \nw 1 1 0 0 \nx 1 0 0 0 \n", g.print_matrix
	end

	# Test if the program works, by throwing input files and comparing to the output
	# To add more test cases, put the files entradan and saidan (where n is an integer) 
	# def test_true_inputs
	# 	n = Dir.entries('Testes').reject{ |f| f.match('^\.') }.size / 2
	# 	(1..n).each do |i|
	# 		system("(./chordal.rb < Testes/entrada#{i}) > /tmp/saida && (diff -q /tmp/saida Testes/saida#{i} > /tmp/diff)")
	# 		assert_equal "", File.open('/tmp/diff').read
	# 	end
	# end

end

Test::Unit::UI::Console::TestRunner.run(GraphTest)
